package com.newstore.jeudedonnees;

import java.util.HashMap;
import java.util.Vector;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.newstore.classes.Barcode;
import com.newstore.classes.Delivery;
import com.newstore.classes.Order;
import com.newstore.classes.Product;
import com.newstore.classes.User;
import com.newstore.database.AccessDatabase;
import com.newstore.database.BarcodeDatabase;
import com.newstore.database.DeliveryDatabase;
import com.newstore.database.OrderDatabase;
import com.newstore.database.ProductDatabase;
import com.newstore.database.UserDatabase;

public class InsertData
{
	public SQLiteDatabase insertionUser(SQLiteDatabase database)
	{
		User userPrepa = new User("prepa", "prepa", "prepa");
		User userExpe = new User("expe", "expe", "expe");
		User userRecep = new User("recep", "recep", "recep");
		UserDatabase usDb = new UserDatabase(database);
		usDb.insertUser(userPrepa);
		usDb.insertUser(userExpe);
		usDb.insertUser(userRecep);

		return database;
	}

	
	
	public SQLiteDatabase insertionDelivery(SQLiteDatabase database){
		Delivery delivery = new Delivery("exia", "3x14", "Quai d'éxédition n°8");
		Delivery delivery2 = new Delivery("Microsoft", "m1cr0s0f7", "Quaie d'éxédition n°3");
		Delivery delivery3 = new Delivery("Google", "g00g13", "Quaie d'éxédition n°12");
		
		DeliveryDatabase pdDb = new DeliveryDatabase(database);
		pdDb.insertDelivery(delivery);
		pdDb.insertDelivery(delivery2);
		pdDb.insertDelivery(delivery3);
		return database;
	}
	
	public SQLiteDatabase insertionProduct(SQLiteDatabase database){
			
		insertProductFunction("Réfrigérateur Bosh", "can1", "IP", "B", 50, database);
		insertProductFunction("Télévision Samsung", "sdfd", "EP", "B", 60, database);
		insertProductFunction("Lave-linge Electrolux", "dfsf", "IP", "B", 78, database);

		return database;
		
	}
	
	public SQLiteDatabase insertionOrder(SQLiteDatabase database)
	{
		ProductDatabase productDatabase = new ProductDatabase(database);
		Product product = productDatabase.getProductFromId(1);
		Product product1 = productDatabase.getProductFromId(2);
		Product product2 = productDatabase.getProductFromId(3);
		
		HashMap<Product, Integer> productHM = new HashMap<Product, Integer>();
		HashMap<Product, Integer> productHM2 = new HashMap<Product, Integer>();
		HashMap<Product, Integer> productHM3 = new HashMap<Product, Integer>();
		productHM.put(product, 3);
		productHM2.put(product1, 2);
		productHM3.put(product2, 5);
		Vector<HashMap<Product, Integer>> contentOrder = new Vector<HashMap<Product,Integer>>();
		contentOrder.add(productHM);
		contentOrder.add(productHM2);
		contentOrder.add(productHM3);
		DeliveryDatabase deliveryDatabase = new DeliveryDatabase(database);
		
		Order firstOrder = new Order("En attente", "Commande FTW", contentOrder, deliveryDatabase.getDeliveryFromId(1));
		
		OrderDatabase usDb = new OrderDatabase(database);
		
		usDb.insertOrder(firstOrder);
		return database;
	}
	
	private void insertProductFunction(String nom, String reference, String location, String category, int stock, SQLiteDatabase database)
	{
		ProductDatabase productDatabase = new ProductDatabase(database);
		BarcodeDatabase barcodeDatabase = new BarcodeDatabase(database);
		
		Product prod = new Product(nom, reference, location, category, stock);
		int idNewProduct = (int)productDatabase.insertProduct(prod);
		String productBarcode = "";
		if (idNewProduct < 10)
			productBarcode = "5000";
		else if (idNewProduct < 100)
			productBarcode = "500";
		else if (idNewProduct < 1000)
			productBarcode = "50";
		
		productBarcode += idNewProduct; 
			
		Barcode barcode = new Barcode(Integer.parseInt(productBarcode));
		int idNewBarcode = (int)barcodeDatabase.insertBarcode(barcode);
		productDatabase.updateProductFromId(idNewProduct, idNewBarcode);
	}
}
